package dao.impl;

import entity.SpecialtyEntity;
import dao.SpecialtyDao;
import util.JDBCUtil;

import java.util.List;
import java.util.Map;

public class SpecialtyDaoImpl extends JDBCUtil implements SpecialtyDao {
    @Override
    public List<Map<String, Object>> getSpecialtyInfo(String name) {
        String sql;
        List<Map<String, Object>> mapList;
        if ("null".equalsIgnoreCase(name)) {
            sql = "select * from specialty_info";
            mapList = queryForList(sql);
        } else if (name != null && !name.trim().isEmpty()) {
            sql = "select * from specialty_info where specialty_name like ?";
            String specialtyName = "%" + name + "%";
            mapList = queryForList(sql, specialtyName);
        } else {
            sql = "select * from specialty_info";
            mapList = queryForList(sql);
        }
        return mapList;
    }

    @Override
    public int addSpecialtyInfo(SpecialtyEntity specialty) {
        String sql = "insert into specialty_info (id,academy_id,specialty_name,specialty_code) values (?,?,?,?)";
        return insert(sql, specialty.getId(), specialty.getAcademyId(), specialty.getSpecialtyName(), specialty.getSpecialtyCode());
    }

    @Override
    public int updateSpecialtyInfo(SpecialtyEntity specialty) {
        String sql = "update specialty_info set academy_id = ?,specialty_name = ?,specialty_code = ? where id = ?";
        return update(sql, specialty.getAcademyId(), specialty.getSpecialtyName(), specialty.getSpecialtyCode(), specialty.getId());
    }

    @Override
    public int deleteSpecialty(String specialtyId) {
        String sql = "delete specialty_info from specialty_info where id = ?";
        return delete(sql, specialtyId);
    }

    @Override
    public int batchDeleteSpecialty(List<String> ids) {
        if (ids == null || ids.isEmpty()) {
            throw new IllegalArgumentException("ID列表不能为空");
        }

        StringBuilder placeholders = new StringBuilder();
        for (int i = 0; i < ids.size(); i++) {
            placeholders.append("?");
            if (i < ids.size() - 1) {
                placeholders.append(",");
            }
        }

        String sql = "delete from specialty_info where id in (" + placeholders + ")";
        return delete(sql, ids.toArray());
    }

    @Override
    public Map<String, Object> getSpecialtyInfoById(String id) {
        String sql = "select * from specialty_info where id = ?";
        return queryForList(sql, id).get(0);
    }

    @Override
    public List<Map<String, Object>> getSpecialtyByAcademyId(String academyId) {
        String sql = "select * from specialty_info where academy_id = ?";
        return queryForList(sql, academyId);
    }

    @Override
    public String getMaxClassCodeBySpecialtyId(String specialtyId) {
        String sql = "select class_code from class_info where specialty_id = ? order by class_code Desc";
        List<Map<String, Object>> result = queryForList(sql, specialtyId);

        if (result.isEmpty()) {
            return null;
        }

        return (String) result.get(0).get("class_code");
    }
}
